//
//  TPQHistoryViewController.m
//  TrafficPeccancyQuery
//
//  Created by Vitas on 2018/7/5.
//  Copyright © 2018年 Bylongma. All rights reserved.
//

#import "MNDTPQHistoryViewController.h"
#import "MNDTrafficPeccancyQueryViewController.h"
#import "MNDTPQHistoryCell.h"
#import "SYRefreshControl.h"
#import "MNDTPQHistoryModel.h"
#import "MNDTPQResultModel.h"
#import "MNDTPQResultViewController.h"

#define kTPQHistoryCell @"MNDTPQHistoryCell"

@interface MNDTPQHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) UIView *placeHolderMaskView;

@end

@implementation MNDTPQHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    kdeclare_weakself;
    [SYRefreshControl configGifMJRefresh:self.tableView refreshType:MJRefreshSupportRefresh beginRefresh:NO timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{
        [weakSelf loadHistoryData];
    } loadMoreBlock:^{
        
    }];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadHistoryData];
}

#pragma mark - 加载数据
- (void)loadHistoryData {
    kdeclare_weakself;
    [APIManager sendRequestWithURL:API_TRAFFICPECCANCY_HISTORY parameters:[MNDSign signedParam] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            weakSelf.dataArray = [MNDTPQHistoryModel mj_objectArrayWithKeyValuesArray:responseModel.data];
            [weakSelf.view configBlankPage:SYBlankPageTypeNoData hasData:weakSelf.dataArray.count reloadButtonBlock:nil];
            [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:weakSelf.dataArray.count];
            [weakSelf.tableView reloadData];
        }else {
            [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:NO];
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:NO];
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}


#pragma mark - 懒加载
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDTPQHistoryModel *model = self.dataArray[indexPath.row];
    MNDTPQHistoryCell *historyCell = [tableView dequeueReusableCellWithIdentifier: kTPQHistoryCell];
    historyCell.historyModel = model;
    return historyCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MNDTPQHistoryModel *model = self.dataArray[indexPath.row];
    [NetworkProgressHUD showHud];
    kdeclare_weakself;
    NSDictionary *dict = @{
                           @"city":model.breakRuleCity,
                           @"city_name":model.breakRuleCityname,
                           @"carno":model.breakRuleCarno,
                           @"engineno":model.breakRuleEngineno,
                           @"carframe":model.breakRuleCarframe,
                           @"whole_carframe":model.breakRuleWholeCarframe,
                           @"whole_engineno":model.breakRuleWholeEngineno
                           };
    [APIManager sendRequestWithURL:API_QUERY_BREAK_RULES parameters:[MNDSign signedParamWithDict:dict] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        [NetworkProgressHUD hideHud];
        if (responseModel.status == ResponseSuccess) {
            MNDTPQResultModel *model = [MNDTPQResultModel mj_objectWithKeyValues:responseModel.data];
            MNDTPQResultViewController *resultVC = [UIStoryboard viewControllerWithIdentifier:@"MNDTPQResultViewController"];
            resultVC.resultModel = model;
            [weakSelf.navigationController pushViewController:resultVC animated:YES];
        }else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
    
}

- (IBAction)queryNewCarAction:(UIButton *)sender {

    MNDTrafficPeccancyQueryViewController *queryVC = [UIStoryboard viewControllerWithIdentifier:@"MNDTrafficPeccancyQueryViewController"];
    [self.navigationController pushViewController:queryVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
